<?php

namespace App\Http\Resources\Train;

use App\Models\Train\Stations\Repositories\Interfaces\StationRepositoryInterface;
use App\Models\Train\Stations\Repositories\StationRepository;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

class StationMapResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     *
     * @return array|Arrayable|JsonSerializable
     */
    public function toArray($request)
    {
        /**
         * @var StationRepository $stationRepo
         */
        $stationRepo = app()->make(StationRepositoryInterface::class);
        return collect($this->resource)->map(function ($station_code) use ($stationRepo) {
            $station = $stationRepo->findOneByCode($station_code);
            return [
                $station_code => [
                    'station_name'    => $station->getStationName(),
                    'station_name_uy' => $station->getStationNameUy(),
                ],
            ];
        })->collapse()->toArray();
    }
}
